package com.saver.modules.sys.service;

import com.saver.common.db.JdbcTool;
import com.saver.common.utils.PageUtils;
import com.saver.common.utils.Query;
import com.saver.common.utils.R;
import com.saver.modules.sys.dao.SysRoleDao;
import com.saver.modules.sys.dao.SysRoleMenuDao;
import com.saver.modules.sys.dao.SysUserRoleDao;
import com.saver.modules.sys.entity.SysRoleEntity;
import com.saver.modules.sys.entity.SysRoleMenuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 角色
 *
 * @author yuxiaolong
 * @email 1005471232@qq.com
 * @date 2018年08月30日 下午10:45
 */
@Service("sysRoleService")
public class SysRoleService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private JdbcTool jdbcTool;

    public List<Map<String, Object>> queryRoleList(Long userId) {
        String sql = "select sr.* from sys_role sr left join sys_user_role sur on sr.role_id = sur.role_id where sur.user_id=" + userId;
        return jdbcTool.list(sql);
    }

    public R queryPage(Map<String, Object> params){
        Object q = params.get("q");
        String sql = "select role_id,role_name,remark from sys_role sr";
        if (q != null && !q.equals("")) {
            sql = sql + " where remark like '" + q + "%' ";
        }
        PageUtils pageUtils = jdbcTool.queryPage(sql, new Query(params));
        return R.ok().put("page", pageUtils);
    }

    public R info( Long roleId) {
        Optional<SysRoleEntity> byId = sysRoleDao.findById(roleId);
        if (byId.isPresent()) {
            SysRoleEntity sysRoleEntity = byId.get();
            //超级管理员有所有菜单权限
            if (roleId == 1) {
                Set<Long> byRoleId = sysRoleMenuDao.findAllMenuId();
                sysRoleEntity.setMenus(byRoleId);
            } else {
                Set<Long> byRoleId = sysRoleMenuDao.findByRoleId(sysRoleEntity.getRoleId());
                sysRoleEntity.setMenus(byRoleId);
            }
            return R.ok().put("data", sysRoleEntity);
        } else {
            return R.error("角色不存在");
        }
    }

    @Transactional
    public R save(SysRoleEntity sysRoleEntity) {
        SysRoleEntity save = sysRoleDao.save(sysRoleEntity);

        Set<Long> menus = sysRoleEntity.getMenus();
        sysRoleMenuDao.deleteAllByRoleId(save.getRoleId());
        Iterator<Long> iterator = menus.iterator();
        while (iterator.hasNext()) {
            Long next = iterator.next();
            SysRoleMenuEntity entity = new SysRoleMenuEntity();
            entity.setRoleId(save.getRoleId());
            entity.setMenuId(next);
            sysRoleMenuDao.save(entity);
        }
        return R.ok();
    }

    @Transactional
    public R delete(Long roleId) {
        if (roleId == 1) {
            return R.error("不允许删除超级管理员");
        }
        jdbcTool.execute("delete from sys_role_menu where role_id=" + roleId);
        sysRoleDao.deleteById(roleId);
        return R.ok();
    }
}
